package com.sunstar.koobaselib.kootl.usercenter.bean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by louisgeek on 2016/7/11.
 */
public  class BaseListBean<T>{


    /**
     * code : 0
     * message : 获取数据成功！
     * info : [{"result":"False","result_code":"86","result_message":"您还不是蔬菜网会员，请与中国蔬菜网管理员联系！","userid":"0","loginid":"kooer11","head_image":"","image_name":"","mobile":"","company_flag":""}]
     */

    private String code;
    private String message;
    private List<T> info;

    /**
     *
     * 可用 推荐方式  类似于gson.fromJson(jsonString, new TypeToken<List<String>>(){}.getType());
     * 好理解

     使用方式：
    TypeToken<BaseListBean<OthersBean>> typeToken=new TypeToken<BaseListBean<OthersBean>>(){};
    BaseListBean<OthersBean> baseListBean5=BaseListBean.fromJsonFive(body,typeToken);
     * @param json
     * @param token
     * @param <T>
     * @return
     */
    public static <T> BaseListBean fromJsonFive(String json,TypeToken<T> token) {
        Gson gson = new Gson();
        Type objectType = token.getType();
        return gson.fromJson(json, objectType);
    }

    public static BaseListBean fromJsonSix(String json,Class cls) {
        Gson gson = new Gson();
        Type objectType = dealParameterizedTypeInner(BaseListBean.class,cls);
        return gson.fromJson(json, objectType);
    }



    private  static ParameterizedType dealParameterizedTypeInner(final Class raw, final Type... args) {
        ParameterizedType parameterizedType= new ParameterizedType() {
            public Type getRawType() {
                return raw;
            }

            public Type[] getActualTypeArguments() {
                return args;
            }

            public Type getOwnerType() {
                return null;
            }
        };
        return parameterizedType;
    }



    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<T> getInfo() {
        return info;
    }

    public void setInfo(List<T> info) {
        this.info = info;
    }


}
